//<p>设计一个支持 <code>push</code> ，<code>pop</code> ，<code>top</code> 操作，并能在常数时间内检索到最小元素的栈。</p>
//
//<p>实现 <code>MinStack</code> 类:</p>
//
//<ul>
//	<li><code>MinStack()</code> 初始化堆栈对象。</li>
//	<li><code>void push(int val)</code> 将元素val推入堆栈。</li>
//	<li><code>void pop()</code> 删除堆栈顶部的元素。</li>
//	<li><code>int top()</code> 获取堆栈顶部的元素。</li>
//	<li><code>int getMin()</code> 获取堆栈中的最小元素。</li>
//</ul>
//
//<p>&nbsp;</p>
//
//<p><strong>示例 1:</strong></p>
//
//<pre>
//<strong>输入：</strong>
//["MinStack","push","push","push","getMin","pop","top","getMin"]
//[[],[-2],[0],[-3],[],[],[],[]]
//
//<strong>输出：</strong>
//[null,null,null,null,-3,null,0,-2]
//
//<strong>解释：</strong>
//MinStack minStack = new MinStack();
//minStack.push(-2);
//minStack.push(0);
//minStack.push(-3);
//minStack.getMin();   --&gt; 返回 -3.
//minStack.pop();
//minStack.top();      --&gt; 返回 0.
//minStack.getMin();   --&gt; 返回 -2.
//</pre>
//
//<p>&nbsp;</p>
//
//<p><strong>提示：</strong></p>
//
//<ul>
//	<li><code>-2<sup>31</sup>&nbsp;&lt;= val &lt;= 2<sup>31</sup>&nbsp;- 1</code></li>
//	<li><code>pop</code>、<code>top</code> 和 <code>getMin</code> 操作总是在 <strong>非空栈</strong> 上调用</li>
//	<li><code>push</code>,&nbsp;<code>pop</code>,&nbsp;<code>top</code>, and&nbsp;<code>getMin</code>最多被调用&nbsp;<code>3 * 10<sup>4</sup></code>&nbsp;次</li>
//</ul>
//<div><div>Related Topics</div><div><li>栈</li><li>设计</li></div></div><br><div><li>👍 1340</li><li>👎 0</li></div>

package com.rising.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
 * 最小栈
 * @author DY Rising
 * @date 2022-07-04 15:43:21
 */
public class P155_MinStack{
    public static void main(String[] args) {
        //测试代码
        MinStack solution = new P155_MinStack().new MinStack();
        solution.push(1);
        solution.push(2);
        System.out.println(solution.top());
        System.out.println(solution.getMin());
        solution.pop();
        System.out.println(solution.getMin());
        System.out.println(solution.top());
    }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MinStack {
    private Integer min;
    private List<Integer> values = new ArrayList<>();
    private List<Integer> mins = new ArrayList<>();

    public MinStack() {
        mins.add(Integer.MAX_VALUE);
    }
    
    public void push(int val) {
        /*if (min == null) min = val;
        this.min = Math.min(min, val);
        this.values.add(val);*/
        values.add(val);
        mins.add(Math.min(mins.get(mins.size() - 1), val));
    }
    
    public void pop() {
        /*if (values.size() == 0) return;
        values.remove(values.size() - 1);
        if (values.size() == 0) {
            min = null;
            return;
        }
        min = values.get(0);
        for (int i = 0; i < values.size(); i++) {
            min = Math.min(min, values.get(i));
        }*/
        //if (values.size() == 0) return;
        values.remove(values.size() - 1);
        mins.remove(mins.size() - 1);
    }
    
    public int top() {
        return values.get(values.size() - 1);
    }
    
    public int getMin() {
        return mins.get(mins.size() - 1);
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */
//leetcode submit region end(Prohibit modification and deletion)

}
